$(function () {
    initRegist();
    $("body").keydown(function (e) {
        if (event.keyCode === 13) {
            loginRuleSubmit.submitForm("ruleForm");
        }
    });
});

//规则校验，成功回调
var loginRuleSubmit = new Vue({
    el: '#login',
    data: function () {
        var self = this;
        return {
            ruleForm: {
                username: '',
                password: '',
                code: ''
            },
            isSubmit: false,
            rules: {
                username: [{
                    required: true,
                    message: '请输入用户名称',
                    trigger: 'blur'
                }],
                password: [{
                    required: true,
                    trigger: 'blur',
                    validator: function (rule, value, callback) {
                        if (value === '') {
                            callback(new Error('请输入密码'));
                        } else {
                            if (self.isSubmit) {
                                self.isSubmit = false;
                                callback(new Error(self.rspMsg));
                            } else {
                                callback();
                            }
                        }
                    }
                }],
                code: [{
                    required: codeEnable,
                    trigger: 'blur',
                    validator: function (rule, value, callback) {
                        if (codeEnable&&!value) {
                            callback(new Error('请输入验证码'));
                        } else {
                            if (self.isSubmit) {
                                self.isSubmit = false;
                                callback(new Error(self.rspMsg));
                            } else {
                                callback();
                            }
                        }
                    }
                }]
            }
        };
    },
    methods: {
        submitForm: function (formName) {
            var self = this;
            this.$refs[formName].validate(function (valid) {
                if (valid) {
                    $.ajax({
                        url: "/login",
                        method: "POST",
                        async: true,
                        data: {
                            "username": loginRuleSubmit.ruleForm.username, //用户名
                            "password": loginRuleSubmit.ruleForm.password, //密码
                            "code": loginRuleSubmit.ruleForm.code
                        },
                        dataType: "json",
                        success: function (data) {
                            self.isSubmit = true;
                            if (data.rspCode === '000000') {
                                $(".error").html('');
                                var resInfo = data.resultData.userInfo;
                                sessionStorage.setItem("loginUrl", window.location.href);
                                sessionStorage.setItem("username", loginRuleSubmit.ruleForm.username);
                                sessionStorage.setItem("token", data.resultData.token);
                                $.fakeLoadershow();
                                console.log("获取token： \n" + sessionStorage.token);
                                location.href = "/home";
                            } else {
                                self.rspMsg = data.rspMsg;
                                self.$refs["ruleForm"].validateField("password");
                                return false;
                            }
                        }
                    });
                } else {
                    return false;
                }
            });
        }
    }
});

function initRegist() {
    // 自定义相关样式信息
    var windowH = $(window).height(),
        registH = $(".regist_up").height(),
        trueH = (windowH - registH) / 2;
    $(".regist").css("max-height", windowH);
    $(".regist_up").css("margin-top", trueH);
    $(".regist_box").show();
}